package com.fy56.platform.logistics.restful.api.controller.gmms;

import com.alibaba.fastjson.JSON;
import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.service.wx.IAreaService;
import com.fy56.platform.logistics.utils.ParamUtil;
import com.fy56.platform.logistics.utils.StringUtil;
import com.wordnik.swagger.annotations.Api;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 区域网点 Controller
 *
 * CreateTime 2019-04-12 09:53:00
 * author lht
 */
@Api(value = "AreaController", description = "区域网点接口")
@RestController
@RequestMapping("/areaController")
public class AreaController {

    // 日志
    private static Logger logger = Logger.getLogger(AreaController.class);


    @Autowired
    private IAreaService iAreaService;

    /**
     * 获得区域信息列表
     *
     * http://localhost:8082/logistics-restful-api/areaController/queryAreaList
     * param = {}
     *
     * pointFlag:0 查所有区域 pointFlag:1 查所有网点 pointFlag:2 查所有非网点
     */
    @RequestMapping(value = "/queryAreaList", method = {RequestMethod.POST, RequestMethod.GET })
    public ResponseEntity queryAreaList(@RequestParam String data){
        ResultModel result = null;
        Long beginTime = System.currentTimeMillis();
        Exception exceptionOut = null;
        try{
            if(StringUtil.isNotEmpty(data)){
                Map<String, Object> paramMap = JSON.parseObject(data);
                String paramsErrorMsg ="";
                String userName = ParamUtil.getStringValue(paramMap.get("userName"));
                if(StringUtil.isEmpty(userName)){
                    paramsErrorMsg = "userName用户名不可为空 ";
                }
                String userPass = ParamUtil.getStringValue(paramMap.get("userPass"));
                if(StringUtil.isEmpty(userPass)){
                    paramsErrorMsg += "userPass密码不可为空 ";
                }
                Integer pointFlag = ParamUtil.getIntValue(paramMap.get("pointFlag"));
                if(pointFlag == null){
                    paramsErrorMsg += "pointFlag区域类型不可为空 ";
                }
                // 2 登录保险公司用户
                if (StringUtil.isEmpty(paramsErrorMsg)) {
                    result = iAreaService.queryAreaListService(userName, userPass, pointFlag);
                }else{
                    result = new ResultModel(101, paramsErrorMsg,null);
                }
            }else{
                result = new ResultModel(101, "获得区域信息列表：参数列表不可为空。",null);
            }
        }catch (Exception ex){
            result = new ResultModel(101, "获得区域信息列表-queryAreaList：" + ex.getMessage(),null);
            logger.error("获得区域信息列表-queryAreaList：",ex);
            exceptionOut = ex;
        }
        outExecuteInfomactionLog("queryAreaList", beginTime, System.currentTimeMillis(),"url", data, result, exceptionOut);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     * 打印输出方法执行信息
     *
     * @param methodName
     * @param beginTime
     * @param endTime
     * @param url
     * @param param
     * @param result
     */
    public static void outExecuteInfomactionLog(String methodName, Long beginTime, Long endTime, String url, String param, Object result, Exception ex){
        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        logger.info("MethodName: " + methodName);
        logger.info("Url: " + url );
        logger.info("Param: " + param);
        logger.info("Exception: " + (ex != null ? ex.getMessage() : "无"));
        logger.info("result: " + result);
        logger.info("BeginTime: " + dataFormat.format(new Date(beginTime)) + "   EndTime: " + dataFormat.format(new Date(endTime)));
        logger.info("ExecuteTime: " + dataFormat.format(new Date(endTime - beginTime)));
    }


}
